package cn.com.jrr.core.service.dictionary;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.dao.impl.BaseDaoImpl;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.PageModel;
import cn.com.jrr.core.model.dictionary.Code;

@Service(value = "codeService")
public class CodeService {
	@Resource
	private BaseDaoImpl<Code> baseDaoImpl;
	@Autowired
	PageModel <Code> pageModel;
	/**
	 * 查询数据
	 * */
	public DataTableResponse<Code> getAll(DataTableRequest dataTableRequest){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="select * from code order by id desc";
		Object [] obj=new String[]{"code","descript"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<Code> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,Code.class);
		return  dataTableResponse;
	}
	public List<Code> list(){
		return baseDaoImpl.findBySql("select * from code", Code.class);
	}
	public List<Code> listByCode(String code){
		return baseDaoImpl.findBySql("select * from code where code = ?", Code.class,new Object[]{code});
	}

	/**
	 * 添加数据
	 * */
	public int add(Code code){
		return baseDaoImpl.updateBySql("insert into code(code,descript) values(?,?)",
				new Object[]{
						code.getCode(),
						code.getDescript()
				});
	}
	/**
	 * 获取一条
	 * */
	public Code get(Integer id){
		return  baseDaoImpl.get(Code.class, id);
	}
	/**
	 * 删除记录
	 * */
	public int delete(Integer id){
		return  baseDaoImpl.updateBySql("delete from code where id=?", new Object[]{id});
	}
	/**
	 * 更新记录
	 * */
	public int update(Code code){
		return  baseDaoImpl.updateBySql("update code set code=?,descript=? where id=?", new Object[]{
				code.getCode(),code.getDescript(),code.getId()});
	}
	/**
	 * getByCode
	 * */
	public Code getByCode(String code){
		List<Code> codes =  baseDaoImpl.findBySql("select * from Code c where c.code=?",Code.class, new Object[]{code});
		if(codes.size()>0){
			return codes.get(0);
		}
		return null;
	}

}
